/*
 * @Author: LGX
 * @Date: 2022/5/3
 * @Description:
 */
import BaseController from '@/lib/base-controller';
//
import { GET, POST, RequestMapping } from '@/lib/add-router';
@RequestMapping('/api/mail')
export default class MailListController extends BaseController {
  @POST('/addMail')
  async addMail() {
    try {
      const { ctx, service } = this;
      const mail = await service.mailList.addMail(ctx.request.body);
      if (!mail) {
        this.fail('新增失败');
        return;
      }
      this.success({
        code: 200,
        msg: '新增成功',
      });
    } catch (e) {
      this.fail(e, 500);
    }
  }
  @POST('/deleteMail')
  async deleteMail() {
    try {
      const { ctx, service } = this;
      const { id } = ctx.request.body;
      const mail = await service.mailList.deleteMail(id);
      if (!mail) {
        this.fail('删除失败');
        return;
      }
      this.success({
        code: 200,
        msg: '删除成功',
      });
    } catch (e) {
      this.fail(e, 500);
    }
  }
  @POST('/updateMail')
  async updateMail() {
    try {
      const { ctx, service } = this;
      const { id } = ctx.request.body;
      const mail = await service.mailList.updateMail(ctx.request.body, id);
      if (!mail) {
        this.fail('修改失败');
        return;
      }
      this.success({
        code: 200,
        msg: '修改成功',
      });
    } catch (e) {
      this.fail(e, 500);
    }
  }
  @GET('/mailLists')
  async mailList() {
    try {
      const { ctx, service } = this;
      // ctx.validate(
      //   {
      //     page: { type: 'int' },
      //     pageSize: { type: 'int' },
      //   },
      //   ctx.query
      // )
      //  console.log(ctx, '322222222222222')
      const param = await service.mailList.getDiary(ctx.query);

      this.success(param);
    } catch (e) {
      this.fail(e, 500);
    }
  }
}
